package random.fox.p2p.presenter;

import android.text.TextUtils;

import java.io.File;

import random.fox.p2p.interactors.MessageInteractor;
import random.fox.p2p.interactors.OnSendMessageListener;
import random.fox.p2p.view.IMessageView;

/**
 * Created by delong on 2015/3/24.
 */
public class SendMessagePresenterImpl implements SendMessagePresenter,OnSendMessageListener {

    IMessageView iMessageView;
    MessageInteractor interactor;

    public SendMessagePresenterImpl(IMessageView iMessageView, MessageInteractor interactor){
        this.iMessageView = iMessageView;
        this.interactor = interactor;
    }


    public void addMessage(String theme, String content, File image){
        if (TextUtils.isEmpty(theme)){
            iMessageView.showMessage("主题不能为空");
            return;
        }else if (TextUtils.isEmpty(content)){
            iMessageView.showMessage("内容不能为空");
            return;
        }else if(image!=null && !image.exists()){  //image不为null,但文件不存在
            iMessageView.showMessage("选择图片出现未知错误");
            return;
        }

        iMessageView.showProgress();
        interactor.addMessage(theme , content , image , this);
    }

    @Override
    public void onSuccess() {
        iMessageView.hideProgress();
        iMessageView.showMessage("添加成功");
        interactor.releaseHotData();
        iMessageView.finishView();
    }

    @Override
    public void onError() {
        iMessageView.hideProgress();
        iMessageView.showMessage("添加失败");
    }
}
